﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = Username.Text;
            string psw = Password.Text;
            string gender = male.Checked ? "0":"1";
            string email = Email.Text;
            string constr = "server=.;uid=xiaocao;psw=123456;database=Student_db";
            SqlConnection con = new SqlConnection(constr);
            string sql = "insert into StudentInfo(stu_name,gender,email,password)values(@name,@gender,@email,@psw)";
            SqlParameter[] pars =
           {
                new SqlParameter("@name",name),
                new SqlParameter("@gender",gender),
                new SqlParameter("@email",email),
                new SqlParameter("@psw",psw)
            };

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddRange(pars);

                int r = cmd.ExecuteNonQuery();

                if (r != -1)
                {
                    Literal1.Text = "注册成功！！！<a href='Login.aspx'>去登入</a>";
                }
                else
                {
                    Literal1.Text = "注册失败！";
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
    }
}